using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using cframework_xlua;
using System.Threading.Tasks;

public static class StartGame
{

    public static IEnumerator LoadSceneAsync(string scenePath, LoadSceneMode mode = LoadSceneMode.Single)
    {
        if(string.IsNullOrEmpty(scenePath))
        {
            yield return false;
        }

        Debug.Log($"check if scene({scenePath}) is in addressable?");
        var h = Addressables.LoadResourceLocationsAsync(scenePath, typeof(UnityEngine.ResourceManagement.ResourceProviders.SceneInstance));
        yield return h;
        if (h.Status == AsyncOperationStatus.Succeeded && h.Result.Count > 0)
        {
            Debug.Log($"load scene({scenePath}) from addressable");

            var handle = Addressables.LoadSceneAsync(scenePath, mode, false);
            yield return handle;
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
                yield return handle.Result.ActivateAsync();
                SceneManager.SetActiveScene(handle.Result.Scene);

                yield return true;
            }
        }

        Debug.Log($"load scene({scenePath}) from SceneManager");
        AsyncOperation ao = SceneManager.LoadSceneAsync(scenePath, mode);
        if (ao == null)
        {
            Debug.LogError("load scene failed: " + scenePath);
            yield return false;
        }

        ao.allowSceneActivation = false;
        while (ao.progress < 0.9f)
        {
            yield return null;
        }

        ao.allowSceneActivation = true;
        yield return ao;

        yield return ao.isDone;
    }


    //static IEnumerator LoadSceneAsyncByIndex(int index, LoadSceneMode mode = LoadSceneMode.Single)
    //{
    //    Debug.Log($"load scene({index}) from SceneManager");
    //    AsyncOperation ao = SceneManager.LoadSceneAsync(index, mode);
    //    if (ao == null)
    //    {
    //        Debug.LogError("load scene failed: " + index);
    //        yield return false;
    //    }

    //    ao.allowSceneActivation = false;
    //    while (ao.progress < 0.9f)
    //    {
    //        yield return null;
    //    }

    //    ao.allowSceneActivation = true;
    //    yield return ao;

    //    yield return ao.isDone;
    //}


    public static IEnumerator StartAsync(string gameScene)
    {
        Debug.Log("StartGame.Start()");

        if (AppManager.GetAppInfo().enable_hotupdate)
        {
            HotUpdateCodeInfo info = HotUpdateDllMgr.latestVersionInfo;
            if (info != null)
            {
                Debug.Log("HotUpdate Code version:" + info.hotupdate_version + " | " + info.build_timestamp);

                var task = HotUpdateDllMgr.LoadBundle().WithResult();
                yield return task;
            }
            else
            {
                throw new System.Exception("HotUpdateCode info is null");
            }
        }

        var loadSceneTask = LoadSceneAsync(gameScene, LoadSceneMode.Additive).WithResult();
        yield return loadSceneTask;
        bool loadSceneSuccess = (bool)loadSceneTask.Current;

        //if(!loadSceneSuccess)
        //{
        //    var loadSceneTask2 = LoadSceneAsyncByIndex(1, LoadSceneMode.Additive).WithResult();
        //    yield return loadSceneTask2;
        //    loadSceneSuccess = (bool)loadSceneTask2.Current;
        //}

        yield return loadSceneSuccess;
    }
}
